package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;
/*
    控制层测试类
 */
public class UserController {
    //创建业务层对象
    private UserService service = new UserServiceImpl();

    //设置全局变量
    private SqlSession session = null;

    //在@Test之前执行
    /*@Before
    public void before() throws Exception {
        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        session = factory.openSession();
    }*/

    //在@Test之后执行
    /*@After
    public void after() {
        //5、事务提交(查询事务用事务，没事务就算了)
        session.commit();
        //6、释放资源
        session.close();
    }*/

    //查询所有用户信息
    @Test
    public void selectAll() throws Exception {
        //4、调用具体的statement对象，执行sql
        List<User> list = service.selectAll();
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
    }

    //根据id查询
    @Test
    public void selectById() throws Exception{
        //4、调用具体的statement对象，执行sql
        User user = service.selectById(2);
        //处理结果
        System.out.println(user);
    }

    //添加用户信息
    @Test
    public void insertUser() throws Exception {
        User user = new User();
        user.setUname("李武");
        user.setUpwd("123");
        //4、调用具体的statement对象，执行sql
        service.insertUser(user);
    }

    //修改用户信息
    @Test
    public void updateUser() throws Exception {
        User user = service.selectById( 8);
        user.setUname("李月");
        user.setUpwd("333");
        user.setSex("女");
        service.updateUser(user);
    }

    //删除用户信息
    @Test
    public void deleteUser() throws Exception {
        service.deleteUser(8);
    }

    //统计人数
    @Test
    public void total() throws Exception {
        //4、调用具体的statement对象，执行sql
        int u = session.selectOne("UserMapper.total");
        //处理结果
        System.out.println(u);
    }

    //模糊查询1
    @Test
    public void findlikeName1() throws Exception {
        //4、调用具体的statement对象，执行sql
        List<User> list = session.selectList("UserMapper.findlikeName","%李%");
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
    }

    //模糊查询2
    @Test
    public void findlikeName2() throws Exception {
        //4、调用具体的statement对象，执行sql
        List<User> list = session.selectList("UserMapper.findlikeName2","李");
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
    }
}
